package com.mujin.keji.collectionsystem.module.main.views;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mujin.keji.collectionsystem.CcsApplication;
import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseFragment;
import com.mujin.keji.collectionsystem.dialog.BaseNiceDialog;
import com.mujin.keji.collectionsystem.dialog.NiceDialog;
import com.mujin.keji.collectionsystem.dialog.ViewConvertListener;
import com.mujin.keji.collectionsystem.dialog.ViewHolder;
import com.mujin.keji.collectionsystem.module.login.dto.LoginDto;
import com.mujin.keji.collectionsystem.module.mypager.viewmodel.LogoutViewModel;
import com.mujin.keji.collectionsystem.module.mypager.views.AboutActivity;
import com.mujin.keji.collectionsystem.module.mypager.views.PersonalDataActivity;
import com.mujin.keji.collectionsystem.module.protectionrules.view.OutRuleActivity;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.mujin.keji.collectionsystem.utils.SPUtil;
import com.mujin.keji.collectionsystem.utils.ToastUtils;
import com.mujin.keji.collectionsystem.widget.CircularImageView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

/**
 * 主页面右边fragment碎片
 *
 * @author weishixiong
 * @Time 2018-03-19
 */
public class RightFragment extends BaseFragment implements View.OnClickListener {
    private CircularImageView ivHead;
    private TextView tvName;
    private TextView tvNumber;
    private LinearLayout llPersonData;
    //    private LinearLayout llUpdatePwd;
    private LinearLayout llAboutApp;
    private RelativeLayout rlLogout;
    private LogoutViewModel logoutViewModel;
    //    private LinearLayout llUpdatePhone;
    private SmartRefreshLayout refreshLayout;
    private LinearLayout llOutRule;


    @Override
    protected void createViewModel() {
        logoutViewModel = ViewModelProviders.of(this).get(LogoutViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_right;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        ivHead = (CircularImageView) view.findViewById(R.id.iv_head);
        //边框大小
        ivHead.setBorderWidth(0);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvNumber = (TextView) view.findViewById(R.id.tv_number);
//        llPersonData = (LinearLayout) view.findViewById(R.id.ll_person_data);
//        llUpdatePwd = (LinearLayout) view.findViewById(R.id.ll_update_pwd);
        llAboutApp = (LinearLayout) view.findViewById(R.id.ll_about_app);
        rlLogout = (RelativeLayout) view.findViewById(R.id.rl_logout);
//        llUpdatePhone = (LinearLayout) view.findViewById(R.id.ll_update_phone);
        llOutRule = (LinearLayout) view.findViewById(R.id.ll_out_rule);

    }

    @Override
    protected void initData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        initPersonData();
    }

    @Override
    protected void loadData() {

    }

    /**
     * 显示用户信息，姓名，用户名，图片
     */
    private void initPersonData() {

        LoginDto dto = SPUtil.getData(Constant.SP.SP, Constant.SP.LOGIN_KEY, LoginDto.class, null);
        tvName.setText(dto == null ? "" : (dto.getUserName() == null ? "" : (dto.getUserName())));
        tvNumber.setText("登录名：" + (dto == null ? "" : (dto.getUserAccount() == null ? "" : (dto.getUserAccount()))));
        if (EmptyUtils.isNotEmpty(dto) && EmptyUtils.isNotEmpty(dto.getImg())) {
            Glide
                    .with(getActivity())
                    .load(dto.getImg())
                    .into(ivHead);
        }

    }

    @Override
    protected void initListener() {
//        llPersonData.setOnClickListener(this);
//        llUpdatePwd.setOnClickListener(this);
        rlLogout.setOnClickListener(this);
//        llUpdatePhone.setOnClickListener(this);
        llOutRule.setOnClickListener(this);
        llAboutApp.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_person_data:
                startActivity(new Intent(getActivity(), PersonalDataActivity.class));
                break;
            case R.id.tv_right_text:
                getActivity().finish();
                break;

            case R.id.ll_about_app:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;


            case R.id.ll_out_rule:
                startActivity(new Intent(getActivity(), OutRuleActivity.class));
                break;
            case R.id.rl_logout:
                NiceDialog.init()
                        .setLayoutId(R.layout.dialog_logout_tip)
                        .setConvertListener(new ViewConvertListener() {
                            @Override
                            public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                                dialog.setOutCancel(false);
                                TextView tvLeft = (TextView) holder.getView(R.id.tv_left);
                                TextView tvRight = (TextView) holder.getView(R.id.tv_right);
                                tvLeft.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        dialog.dismiss();
                                    }
                                });
                                tvRight.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        CcsApplication.logout(Constant.Server.I_LOGOUT);
                                        ToastUtils.showToast("退出登录成功");
                                    }
                                });

                            }
                        })
                        .show(getActivity().getSupportFragmentManager());


                break;
        }
    }
}
